def is_prime(sigma):
  if sigma <= 1:
    return False
  for z in range(2, int(sigma**0.5) + 1):
    if sigma % z == 0:
      return False
  return True

def get_max_prime(sigma):
  if not 1000 <= sigma <= 9999:
    return 0
  digits = list(str(sigma))
  max_prime = 0
  for z in range(len(digits) - 1, -1, -1):
    for d in range(z, -1, -1):
      number = int("".join(digits[d:z+1]))
      if is_prime(number) and number > max_prime:
        max_prime = number
  return max_prime

sigma = int(input("Введите четырёхзначное натуральное число: "))
result = get_max_prime(sigma)
print(result)